package com.example.exception;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.example.common.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常捕获，将捕获到的异常信息返回给前端
 */
@ControllerAdvice("com.example.controller")//异常检测的包
public class GlobalExceptionHandler {

    private static final Log log = LogFactory.get();//日志对象

    @ExceptionHandler(Exception.class)
    @ResponseBody //返回json
    public Result error(Exception e){
        log.error("异常信息：",e);
        return Result.error();
    }

    @ExceptionHandler(CustomException.class)
    @ResponseBody //返回json
    public Result error(CustomException e){
//        log.error("异常信息：",e);
        return Result.error(e.getCode(),e.getMsg());
    }
}
